﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Sisocana.Core.Models;
using Sisocana.Core.Interfaces;
using Sisocana.Core;
using System.IO;
using Sisocana.Seguridad;

namespace Sisocana.Controllers
{
    public class ImagenController : Controller
    {
       private IFuenteService _fuenteService;
        
        public ImagenController()
            : this(new FuenteService())
        {
        }

        public ImagenController(IFuenteService fuenteService)
        {
            _fuenteService = fuenteService;
        }

        //
        // GET: /Imagen/
        [Authorize]
        public ActionResult Index()
        {
            IList<ImagenModel> redes = _fuenteService.GetAllImagenes();
            return View(redes);      
        }

        //
        // GET: /Imagen/Create
        [Authorize]
        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Imagen/Create
        [Authorize]
        [HttpPost]
        public ActionResult Create([Bind(Exclude = "logo")]ImagenModel imgs)
        {
            try
            {
                HttpPostedFileBase logo = Request.Files["logo"];


                ImagenModel imgadd = new ImagenModel();
                imgadd.nombre = imgs.nombre;
                imgadd.pagWeb = imgs.pagWeb;
                imgadd.descripcion = imgs.descripcion;


                if (logo != null && logo.ContentLength > 0)
                {
                    imgadd.logoNomb = logo.FileName;
                    imgadd.logoExt = logo.ContentType;

                    byte[] img = new byte[logo.InputStream.Length];
                    logo.InputStream.Read(img, 0, img.Length);
                    imgadd.logo = img;
                }

                if (_fuenteService.addImage(imgadd) == 0)
                    return RedirectToAction("Index");
                else
                    return View();
            }
            catch(Exception e)
            {
                return View();
            }
        }
        
        //
        // GET: /Imagen/Edit/5
        [AdminGralAuthorize]
        public ActionResult Edit(string id)
        {
            ImagenModel rs = _fuenteService.getSigleImagen(a => a.nombre.Trim() == id);
            return View(rs);
        }

        //
        // POST: /Imagen/Edit/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Edit([Bind(Exclude = "logo")]ImagenModel imgs)
        {
            try
            {
                HttpPostedFileBase logo = Request.Files["logo"];

                if (logo != null && logo.ContentLength > 0)
                {
                    imgs.logoNomb = logo.FileName;
                    imgs.logoExt = logo.ContentType;

                    byte[] img = new byte[logo.InputStream.Length];
                    logo.InputStream.Read(img, 0, img.Length);
                    imgs.logo = img;
                }

                if (_fuenteService.updateImage(imgs) == 0)
                    return RedirectToAction("Index");
                else
                    return View();
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Imagen/Delete/5
        [AdminGralAuthorize]
        public ActionResult Delete(string id)
        {
            ImagenModel rs = _fuenteService.getSigleImagen(a => a.nombre.Trim() == id);
            return View(rs);
        }

        //
        // POST: /Imagen/Delete/5
        [AdminGralAuthorize]
        [HttpPost]
        public ActionResult Delete([Bind(Exclude = "logo")]ImagenModel img)
        {
            try
            {
                int res =_fuenteService.deleteImage(img);
                if (res == 0)
                    return RedirectToAction("Index");
                else
                {
                   return RedirectToAction("MessageID","Error",new{ errorid=res} );
                }
            }
            catch
            {
                return RedirectToAction("MessageID", "Error", new { errorid = -110 });
            }
        }


        public void Imagen(string id)
        {
            ImagenModel rs = _fuenteService.getSigleImagen(a => a.nombre.Trim() == id);
            Response.ContentType = rs.logoExt;
            Response.BinaryWrite(rs.logo);
        }
    }
}
